<?php
namespace app\admin\controller\system;

use app\common\controller\AdminController;
use think\facade\Session;
use think\App;
use think\facade\Db;
use think\facade\View;

/**
 * @ControllerAnnotation 聊天记录
 * Class Auth
 * @package app\admin\controller\system
 */
class Chat extends AdminController
{

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = app('app\common\model\chat\ChatMessage');
        $this->store = app('app\common\model\mall\Store');
        $this->user = app('app\common\model\user\WxUsers');
        $this->admin = app('app\common\model\system\SystemAdmin');

    }
    /**
     * 获取附带数据
     * @param Request $request
     * @return mixed
     */
    public function otherData($request)
    {
        //当前账户id
        $account_id = 11;
        $user_list = $this->model->where(['to_id'=>$account_id,'type'=>1])->distinct(true)->column('from_id');
        $account_list = $this->model->where('type',1)->distinct(true)->column('to_id');

        if($account_list){
            $admin_list = $this->admin->distinct(true)->column('store_id','id');


            foreach($account_list as $k=>$v){
                if(array_key_exists($v, $admin_list)){
                    $account_lists[$k]['name'] = $this->store->where('id',$admin_list[$v])->value('name');
                    $account_lists[$k]['id'] = $v;
                }

            }


            View::assign('account_lists',$account_lists);
        }

    }

    /**
     * 获取商家聊天列表
     * @param Request $request
     */
    public function getData()
    {
        //当前账户id
        $account_id = $this->request->param('account_id',11);

        $sub = $this->model
            ->field('to_id as uid, content,type,msgtype,see_status,create_time')
            ->where('from_id ='.$account_id)
            ->union('SELECT from_id as uid,content,type,msgtype,see_status,create_time FROM bgt_chat_message WHERE to_id = '.$account_id)
            ->order('create_time desc')
            ->buildSql();

        $query = Db::table($sub)
            ->alias('tmp')
            ->group('tmp.uid')
            ->order('tmp.create_time desc')
            ->buildSql();

        $res = Db::table($query)
            ->alias('t')
            ->field('uid,nick_name,avatar_url,content,type,msgtype,see_status,t.create_time')
            ->join('bgt_wx_users u', 't.uid=u.id', 'LEFT')
            ->order('t.create_time desc')
            ->select()->toArray();

       // dd(base64_decode('7aC97biP7aC97bic'));
        foreach($res as $k=>$v){
            //var_dump(base64_decode($v['content']));

            $res[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            /*if($v['msgtype'] !=3){
                $res[$k]['content'] = base64_decode($v['content']);
            }*/
        }

        $data = [
            'code' => 0,
            'msg' => '正在请求中...',
            'count' => count($res),
            'data' => $res,
        ];
        return json($data);

    }

    /**
     * 聊天详情
     * @param Request $request
     * @return View
     */
    public function detail()
    {

        $account_id = $this->request->param('from',11);
        //$account_id = 11;
        $to_id = $this->request->param('to_id');

        $res = $this->model->field('id,content,create_time,msgtype,type,to_id,from_id')
            //->name('chat_message')
            ->where(['to_id'=>$to_id,'from_id'=>$account_id])
            ->union(function($query) use($to_id,$account_id){
                $query->field('id,content,create_time,msgtype,type,to_id,from_id')
                    ->name('chat_message')
                    ->where(['from_id'=>$to_id,'to_id'=>$account_id]);
            })->order('create_time','asc')->select()->toArray();

        $username = $this->user->where('id',$to_id)->value('nick_name');
        $store_id = $this->admin->where('id',$account_id)->value('store_id');
        $adminame = $this->store->where('id',$store_id)->value('name');

        return view('',compact('res','account_id','username','adminame'));
    }

}